package com.quanyan.ic.datacenter.dao.impl;

import com.quanyan.ic.datacenter.constant.Constants;
import com.quanyan.ic.datacenter.dao.AlarmTimePointReportOfPlaceDao;
import com.quanyan.ic.datacenter.http.req.ReqReport;
import com.quanyan.ic.datacenter.http.resp.RespAlarmsTimePointLineChart;
import com.quanyan.ic.datacenter.mapper.icData.AlarmTimePointReportOfPlaceMapper;
import com.quanyan.ic.datacenter.mapper.icData.entity.AlarmTimePointReportOfPlace;
import com.quanyan.ic.datacenter.mapper.icData.entity.AlarmTimePointReportOfPlaceExample;
import com.quanyan.ic.datacenter.model.AlarmDayReportParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/8/23.
 */
@Repository
public class AlarmTimePointReportOfPlaceDaoImpl implements AlarmTimePointReportOfPlaceDao {

    @Autowired
    private AlarmTimePointReportOfPlaceMapper alarmTimePointReportOfPlaceMapper;

    @Override
    public List<RespAlarmsTimePointLineChart> findAlarmTimePointReportOfPlaceByDay(ReqReport reqReport) {
        AlarmTimePointReportOfPlaceExample alarmTimePointReportOfPlaceExampleExample = new AlarmTimePointReportOfPlaceExample();
        AlarmTimePointReportOfPlaceExample.Criteria criteria = alarmTimePointReportOfPlaceExampleExample.createCriteria();
        criteria.andCityIdEqualTo(reqReport.getCityId());
        criteria.andDateEqualTo(reqReport.getStartDate());
        List<AlarmTimePointReportOfPlace> listDb =  alarmTimePointReportOfPlaceMapper.selectByExample(alarmTimePointReportOfPlaceExampleExample);
        List<RespAlarmsTimePointLineChart> result = new ArrayList<>();
        if ( listDb != null && listDb.size() > 0 ){
            for ( AlarmTimePointReportOfPlace db : listDb ){
                RespAlarmsTimePointLineChart respAlarmsTimePointLineChart = new RespAlarmsTimePointLineChart();
                respAlarmsTimePointLineChart.setId(db.getPlaceId());
                respAlarmsTimePointLineChart.setName(db.getPlaceName());

               // respAlarmsTimePointLineChart.getVideoLineChart().add(db.getvZero());
                respAlarmsTimePointLineChart.getVideoLineChart().add(db.getvThree());
                respAlarmsTimePointLineChart.getVideoLineChart().add(db.getvSix());
                respAlarmsTimePointLineChart.getVideoLineChart().add(db.getvNine());
                respAlarmsTimePointLineChart.getVideoLineChart().add(db.getvTwelve())
                ;respAlarmsTimePointLineChart.getVideoLineChart().add(db.getvFifteen());
                respAlarmsTimePointLineChart.getVideoLineChart().add(db.getvEighteen());
                respAlarmsTimePointLineChart.getVideoLineChart().add(db.getvTwelve());

             //   respAlarmsTimePointLineChart.getLightLineChart().add(db.getlZero());
                respAlarmsTimePointLineChart.getLightLineChart().add(db.getlThree());
                respAlarmsTimePointLineChart.getLightLineChart().add(db.getlSix());
                respAlarmsTimePointLineChart.getLightLineChart().add(db.getlNine());
                respAlarmsTimePointLineChart.getLightLineChart().add(db.getlTwelve());
                respAlarmsTimePointLineChart.getLightLineChart().add(db.getlFifteen());
                respAlarmsTimePointLineChart.getLightLineChart().add(db.getlEighteen());
                respAlarmsTimePointLineChart.getLightLineChart().add(db.getlTwentyOne());

              //  respAlarmsTimePointLineChart.getOrderLineChart().add(db.getoZero());
                respAlarmsTimePointLineChart.getOrderLineChart().add(db.getoThree());
                respAlarmsTimePointLineChart.getOrderLineChart().add(db.getoSix());
                respAlarmsTimePointLineChart.getOrderLineChart().add(db.getoNine());
                respAlarmsTimePointLineChart.getOrderLineChart().add(db.getoTwelve());
                respAlarmsTimePointLineChart.getOrderLineChart().add(db.getoFifteen());
                respAlarmsTimePointLineChart.getOrderLineChart().add(db.getoEighteen());
                respAlarmsTimePointLineChart.getOrderLineChart().add(db.getoTwentyOne());
                result.add(respAlarmsTimePointLineChart);
            }
        }
        return result;
    }


    public void insertAlarmTimePointPlace(AlarmDayReportParam param){
        Integer placeId = param.getPlaceId();
        Integer cityId = param.getCityId()  ;
        Date date = param.getDate();
        Byte type = param.getType();
        String cityName = param.getCityName();
        String placeName = param.getPlaceName();
        Integer timeInt = param.getStartTimeInt();
        AlarmTimePointReportOfPlaceExample example = new AlarmTimePointReportOfPlaceExample();
        AlarmTimePointReportOfPlaceExample.Criteria criteria = example.createCriteria();
        criteria.andPlaceIdEqualTo(placeId);
        criteria.andDateEqualTo(date);
        List<AlarmTimePointReportOfPlace> listDb =  alarmTimePointReportOfPlaceMapper.selectByExample(example);
        if ( listDb != null && listDb.size() > 0 ){
            AlarmTimePointReportOfPlace place = listDb.get(0);
            AlarmTimePointReportOfPlace update = new AlarmTimePointReportOfPlace();
            update.setId(place.getId());

            if ( timeInt >= 0 && timeInt < 6){
                if ( type == Constants.AlarmsType.order ){
                    update.setoThree( place.getoThree() + 1 );
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    update.setvThree( place.getvThree() + 1 );
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    update.setlThree( place.getlThree() + 1 );
                }

            } else
            if ( timeInt >= 6 && timeInt < 9){

                if ( type == Constants.AlarmsType.order ){
                    update.setoSix(place.getoSix() + 1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    update.setvSix(place.getvSix() + 1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    update.setlSix(place.getlSix() + 1);
                }

            } else if ( timeInt >= 9 && timeInt < 12){
                if ( type == Constants.AlarmsType.order ){
                    update.setoNine(place.getoNine() + 1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    update.setvNine(place.getvNine() + 1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    update.setlNine(place.getlNine() + 1);
                }
            } else if ( timeInt >= 12 && timeInt < 15){
                if ( type == Constants.AlarmsType.order ){
                    update.setoTwelve(place.getoTwelve() + 1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    update.setoTwentyOne(place.getoTwelve() + 1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    update.setlTwelve(place.getlTwelve() + 1);
                }
            }  else if ( timeInt >= 15 && timeInt < 18){
                if ( type == Constants.AlarmsType.order ){
                    update.setoFifteen(place.getoFifteen() + 1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    update.setvFifteen(place.getvFifteen() + 1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    update.setlFifteen(place.getlFifteen() + 1);
                }
            } else if ( timeInt >= 18 && timeInt < 24){
                if ( type == Constants.AlarmsType.order ){
                    update.setoEighteen(place.getoEighteen() + 1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    update.setvEighteen(place.getvEighteen() + 1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    update.setlEighteen(place.getlEighteen() + 1);
                }
            }

            alarmTimePointReportOfPlaceMapper.updateByPrimaryKeySelective(update);

        }else {
            AlarmTimePointReportOfPlace insert = new AlarmTimePointReportOfPlace();
            insert.setCityId(cityId);
            insert.setPlaceId(placeId);
            insert.setPlaceName(placeName);
            insert.setDate(date);
            insert.setCityName(cityName);

            if ( timeInt >= 0 && timeInt < 6){
                if ( type == Constants.AlarmsType.order ){
                    insert.setoThree( 1 );
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    insert.setvThree( 1 );
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    insert.setlThree(1 );
                }

            } else if ( timeInt >= 6 && timeInt < 9){
                if ( type == Constants.AlarmsType.order ){
                    insert.setoSix(1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    insert.setvSix( 1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    insert.setlSix( 1);
                }
            } else if ( timeInt >= 9 && timeInt < 12){
                if ( type == Constants.AlarmsType.order ){
                    insert.setoNine( 1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    insert.setvNine( 1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    insert.setlNine( 1);
                }
            } else if ( timeInt >= 12 && timeInt < 15){
                if ( type == Constants.AlarmsType.order ){
                    insert.setoTwelve( 1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    insert.setoTwentyOne(1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    insert.setlTwelve( 1);
                }
            }  else if ( timeInt >= 15 && timeInt < 18){
                if ( type == Constants.AlarmsType.order ){
                    insert.setoFifteen( 1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    insert.setvFifteen(1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    insert.setlFifteen(1);
                }
            } else if ( timeInt >= 18 && timeInt < 24){
                if ( type == Constants.AlarmsType.order ){
                    insert.setoEighteen(1);
                }else if( type == Constants.AlarmsType.alarms_type_video ){
                    insert.setvEighteen(1);
                } else if( type == Constants.AlarmsType.alarms_type_light ){
                    insert.setlEighteen(1);
                }
            }
            alarmTimePointReportOfPlaceMapper.insertSelective(insert);
        }
    }
}
